<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/12/20
 */

namespace app\models\store;

use app\admin\model\system\ShippingTemplatesFree;
use app\admin\model\system\ShippingTemplatesRegion;
use crmeb\basic\BaseModel;
use think\facade\Cache;
use crmeb\traits\ModelTrait;
use think\facade\Log;
use app\models\system\SystemStore;
use app\models\routine\RoutineTemplate;
use app\models\user\{User, UserAddress, UserBill, UserRecharge, WechatUser};
use crmeb\services\{
    SystemConfigService, WechatTemplateService, workerman\ChannelService
};
use crmeb\repositories\{
    GoodsRepository, PaymentRepositories, OrderRepository, ShortLetterRepositories, UserRepository
};
use app\admin\model\system\ShippingTemplates;

/**
 * TODO 订单Model
 * Class StoreOrder
 * @package app\models\store
 */
class Proxy extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'proxy_config';

    use ModelTrait;


    /**
     * 代理升级
     */
    public static function setLevelComplete($rechargeId){
        //充值信息
        $recharge_info = UserRecharge::get(['id'=>$rechargeId]);
        //代理配置信息
        $proxy_info    = Proxy::get(['mer_id'=>$recharge_info['mer_id']]);
        //用户信息
        $user_info     = User::get(['uid'=>$recharge_info['uid']]);
        //累计充值
        $recharge_total = UserRecharge::where('uid',$recharge_info['uid'])->sum('price');

        $res = true;
        //用户初次成为代理
        if($user_info['proxy_level']==0){
            if($recharge_total>=$proxy_info['three_recharge']){
                $ups = ['proxy_level'=>3];
            }
            if($recharge_total>=$proxy_info['two_recharge']){
                $ups = ['proxy_level'=>2];
            }
            if($recharge_total>=$proxy_info['one_recharge']){
                $ups = ['proxy_level'=>1];
            }
            User::where('uid',$recharge_info['uid'])->save($ups);
        }else {
            //二级代理升级成为一级代理
            if ($recharge_total > $proxy_info['two_recharge']) {
              //修改代理等级，则该代理下面会员的相应代理也修改
                $maps2['level_two_uid'] = $recharge_info['uid'];
                $change_uid2 =  User::where($maps2)->column('uid');
                if($change_uid2) {
                    $ups2['level_two_uid'] = 0;
                    User::update($ups2, $maps2);
                    //2再修改等级
                    $maps11[] = ['uid', 'in', $change_uid2];
                    $ups11['level_one_uid'] = $recharge_info['uid'];
                    User::update($ups11, $maps11);
                }
            }elseif ($recharge_total > $proxy_info['three_recharge']){    //三级代理升级成为二级代理
                //修改代理等级，则该代理下面会员的相应代理也修改
                $maps3['level_three_uid'] = $recharge_info['uid'];
                $change_uid3 =  User::where($maps3)->column('uid');
                if($change_uid3) {
                    $ups3['level_three_uid'] = 0;
                    User::update($ups3, $maps3);
                    //2再修改等级
                    $maps33[] = ['uid', 'in', $change_uid3];
                    $ups33['level_two_uid'] = $recharge_info['uid'];
                    User::update($ups33, $maps33);
                }
            }
        }
        return $res;

    }



}